public class CustomAttachment
{
public Attachment myAttachment {get;set;}
public string selectattType{get;set;}
public string attachmentid{get;set;}
public string description{get;set;}
public CustomAttachment()
{
string  AttachId=ApexPages.currentPage().getParameters().get('AttachId');
attachmentid=AttachId;
if(AttachId!=null)
{
List<Opportunity_Attachments_Classification__c> Lstattchclass=[select id,Attachment_Type__c from Opportunity_Attachments_Classification__c where AttachmentId__c=:AttachId];
if(Lstattchclass.size()>0)
{
Opportunity_Attachments_Classification__c OppAttClass=Lstattchclass[0];
selectattType=OppAttClass.Attachment_Type__c;
}
myAttachment=[select id,body,name,description from Attachment where id=:AttachId];
description=myAttachment.description;
system.debug('testdesc'+description);
}
else
myAttachment = new Attachment();

}
public List<SelectOption> getAttachType()
{
  List<SelectOption> options = new List<SelectOption>();
  options.add(new SelectOption('','--None--'));    
   Schema.DescribeFieldResult fieldResult = Opportunity_Attachments_Classification__c.Attachment_Type__c.getDescribe();
   List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();      
   for( Schema.PicklistEntry f : ple)
   {
      options.add(new SelectOption(f.getLabel(), f.getValue()));
   }       
   return options;
}
public PageReference Cancel()
{
string  oppId=ApexPages.currentPage().getParameters().get('oppId');
PageReference pgref=new PageReference('/'+oppId);
return pgref;
}
public PageReference save()
{
string  oppId=ApexPages.currentPage().getParameters().get('oppId');
string  AttachId=ApexPages.currentPage().getParameters().get('AttachId');
 if (selectattType == null) 
 {
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please select attachment Type'));
    return null;            
 } 
  if (myAttachment.body== null) 
 {
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please upload file'));
    return null;            
 } 
//myAttachment = new Attachment();
if(AttachId!=null)
{
List<Opportunity_Attachments_Classification__c> Lstattchclass=[select id,Attachment_Type__c from Opportunity_Attachments_Classification__c where AttachmentId__c=:AttachId];
Opportunity_Attachments_Classification__c OppAttClass=Lstattchclass[0];
OppAttClass.Attachment_Type__c=selectattType;
update OppAttClass;
myAttachment.description=description;
update myAttachment;
}
else
{
myAttachment.ParentId =oppId;
myAttachment.description=description;
insert myAttachment;
system.debug('testdescrp'+description);
Opportunity_Attachments_Classification__c oppAttType=new Opportunity_Attachments_Classification__c();
oppAttType.AttachmentId__c=myAttachment.Id;
oppAttType.Attachment_Type__c=selectattType;
oppAttType.Opportunity__c=oppId;
insert oppAttType;
}
PageReference pgref=new PageReference('/'+oppId);
return pgref;
}

}